<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="序号" prop="soId">
        <el-input v-model="queryParams.code" placeholder="请输入序号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="柜子类型" prop="name">
        <el-input v-model="queryParams.containerType" placeholder="请输入柜型" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['segmentation:seg-base-sku:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['segmentation:seg-base-sku:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['segmentation:seg-base-sku:removed']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['segmentation:seg-base-sku:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="SegTypeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="序号" align="center" prop="code" />
      <el-table-column label="柜型" align="center" prop="containerType" />
      <el-table-column label="方向" align="center" prop="direction" >
        <template slot-scope="scope">
          {{ scope.row.direction === 1 ? '右' : '左'}}
        </template>
      </el-table-column>
      <el-table-column label="分割坐标点" align="center">
        <template slot-scope="scope">
          <div v-for="(item, index) in scope.row.setPointX.split(',')" :key="index" >
            ({{ item }},{{ scope.row.setPointY.split(',')[index] }})</div>
            <!-- <div>x:{{scope.row.setPointX.split(',')}}</div>
            <div>y:{{scope.row.setPointY}}</div> -->
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['segmentation:seg-base-sku:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['segmentation:seg-base-sku:removed']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改风幕柜类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="序号" prop="code">
          <el-input v-model="form.code" placeholder="请输入序号" />
        </el-form-item>
        <el-form-item label="柜型" prop="containerType">
          <el-input v-model="form.containerType" placeholder="请输入柜型" />
        </el-form-item>
        <el-form-item label="方向" prop="direction">
          <el-select v-model="form.direction" placeholder="请选择" clearable>
            <el-option
              v-for="dict in dict.type.direction_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <div style="display: flex;flex-wrap: wrap;justify-content: space-between;">
          <div v-for="(item, index) in form.setPointX" :key="index"
          style="background-color:#eEeeee;margin-top:5px;padding:5px 20px;border-radius:5px;width:48%">
          <div v-if="index > 2" style="margin-top:5px">
            <span class="el-icon-remove" @click="delPointItem(item, index)"></span>
          </div>
          
          <div v-else style="margin-top:20px"></div>
          <el-form-item :label="`坐标点` + (index + 1)">
            X:<el-input v-model="form.setPointX[index]" type="number" min="0" style="width:70px;margin-right: 20px"
              placeholder="请输入x坐标点" />
            Y:<el-input v-model="form.setPointY[index]" type="number" min="0" style="width:70px"
              placeholder="请输入y坐标点" />
          </el-form-item>
          <!-- <el-form-item :label="`坐标点` + (index + 1)" :prop="form.pointlist[index].x">
            <el-input v-model="item.x" placeholder="请输入坐标点"/>
          </el-form-item> -->
        </div>
        </div>
        
        <div class="addBtn" @click="addPontItem">
          <el-button size="mini" type="text" icon="el-icon-edit">
            新增
          </el-button>
        </div>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSegContainerType, getSegContainerType, removedSegContainerType, addSegContainerType, updateSegContainerType } from "@/api/segmentation/seg-container-type-config";

export default {
  name: "SegContainerType",
  dicts: ['direction_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分割算法SKU表格数据
      SegTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageIndex: 0,
        pageNum: 1,
        pageSize: 20,
        code: null,
        containerType: null,
      },
      // 表单参数
      form: {
        id: null,
        code: null,
        containerType: null,
        setPointX: [3, 4, 5],
        setPointY: [4, 5, 6],
      },
      xlist: [],
      ylist: [],
      // 表单校验
      rules: {
        code: [
          {
            pattern: /^\d+$/,
            message: "请输入正确的数字编号",
            trigger: "blur"
          },
          { required: true, message: "请输入序号", trigger: "blur" }
        ],
        containerType: [
          { required: true, message: "柜型不能为空", trigger: "blur" }
        ],
        direction: [
          { required: true, message: "请选择方向", trigger: "blur" }
        ],
      },
      coordinate: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 新增测试项
    addPontItem() {
      this.form.setPointX.push(0);
      this.form.setPointY.push(0);
      this.$forceUpdate();
    },
    // 删除测试项
    delPointItem(item, index) {
      if (this.form.setPointX.length > 2) {
        // 删除
        this.form.setPointX.splice(index, 1);
        this.form.setPointY.splice(index, 1);
      } else {
        this.$message.warning('至少选择一项');
      }
      this.$forceUpdate();
    },
    /** 查询分割算法SKU列表 */
    getList() {
      this.loading = true;
      this.queryParams.pageIndex = this.queryParams.pageNum - 1;
      listSegContainerType(this.queryParams).then(response => {
        if (response.code === 200) {
          this.SegTypeList = response.rows;

          this.total = response.total;
        } else {
          this.$message.error("接口调用失败,原因：" + response.msg)
        }
      }).finally(() => {
        console.log('this.xlist', this.SegTypeList);
        for (let i = 0; i < this.SegTypeList.length; i++) {
          let y = this.SegTypeList[i].setPointX.split(',');
          let x = this.SegTypeList[i].setPointY.split(',');
          this.xlist.push(x)
          this.ylist.push(y)
        }
        console.log('this.xlist',this.xlist);
        

        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        containerType: null,
        setPointX: [0, 0, 0],
        setPointY: [0, 0, 0],
        remark: null,
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 0;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageIndex: 0,
        pageNum: 1,
        pageSize: 20,
        code: null,
        containerType: null,
      };
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加风幕柜类型";
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      this.reset();

      const id = row.id || this.ids
      await getSegContainerType(id).then(res => {
        let x = res.data.setPointX.split(",");
        let y = res.data.setPointY.split(",");
        this.form = {
          code: res.data.code,
          id: res.data.id,
          containerType: res.data.containerType,
          setPointX: x,
          setPointY: y,
          remark: res.data.remark,
          direction: res.data.direction === 1 ? '右' : '左'
        }
        this.open = true;
        this.title = "修改风幕柜类型";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        let setPointX = this.form.setPointX.toString();
        let setPointY = this.form.setPointY.toString();;
        var params = {
          id: this.form.id,
          code: this.form.code,
          containerType: this.form.containerType,
          direction: this.form.direction,
          remark: this.form.remark,
          setPointX,
          setPointY,
        }
        if (valid) {
          if (this.form.id != null) {
            updateSegContainerType(params).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch(error => {
              this.$modal.msgError("新增失败");
            });;
          } else {
            addSegContainerType(params).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).catch(error => {
              this.$modal.msgError("新增失败");
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除风幕柜编号为"' + ids + '"的数据项？').then(function () {
        // 修改为逻辑删除
        return removedSegContainerType(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch((err) => {
        this.$modal.msgError("删除失败", err);
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/segmentationAlgo/seg-container-type-config/export', {
        ...this.queryParams
      }, `seg-container-type-config_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.addBtn {
  display: flex;
  justify-content: flex-end;
}
</style>